package com.sikadwafastdaisfondturbo.pretloan.base

import android.app.Activity
import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import androidx.fragment.app.Fragment
import com.sikadwafastdaisfondturbo.pretloan.databinding.RootviewBinding
import com.sikadwafastdaisfondturbo.pretloan.utils.BarUtils
import com.sikadwafastdaisfondturbo.pretloan.utils.config.Configs
import com.sikadwafastdaisfondturbo.pretloan.utils.log.HHLog

/**
 * @Author：wwdeng
 * @Time：2024/4/14 18:16
 * <pre>
 * 项目中的Fragment基类
</pre> *
 */
abstract class BaseFragment : Fragment(), IBaseView {

    lateinit var rootViewBinding: RootviewBinding

    /* 是否拦截返回按钮，默认不拦截直接返回 */
    private var isInterceptBack = false

    private var mActivity: Activity? = null

    override fun onAttach(context: Context) {
        super.onAttach(context)
        mActivity = activity
    }

    override fun getContext(): Context? {
        return mActivity
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        rootViewBinding = RootviewBinding.inflate(inflater)
        rootViewBinding.titlebarLayout.setPadding(0, BarUtils.getStatusBarHeight(),0,0)
        rootViewBinding.containerLayout.addView(getRootView(inflater), FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT))
        return rootViewBinding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        if (savedInstanceState != null){
            return
        }
        setTitleBarVisibility(View.GONE)
        //默认不显示切换语言
        setLangBtnVisibility(View.GONE)
        if (Configs.isTanzania()){
            //坦桑地区，显示切换语言图标
            setLangBtnVisibility(View.VISIBLE)
        }
        //返回
        rootViewBinding.ivBaseBack.setOnClickListener {
            onBackAction()
        }

        initView()
        initListener()
        initData()
    }

    /**
     * 获取布局文件根视图
     *
     * @return
     */
    protected abstract fun getRootView(inflater: LayoutInflater): View

    /**
     * 初始化 View 控件
     */
    protected abstract fun initView()

    /**
     * 事件监听
     */
    protected abstract fun initListener()

    /**
     * 加数据
     */
    protected abstract fun initData()

    /**
     * @功能描述：是否显示Toolbar
     */
    fun setTitleBarVisibility(visibility: Int) {
        rootViewBinding.titlebarLayout.visibility = visibility
    }


    /**
     * 设置toolBar背景颜色
     */
    fun setTitleBarColor(color: Int) {
        setTitleBarVisibility(View.VISIBLE)
        rootViewBinding.titlebarLayout.setBackgroundColor(color)
    }

    /**
     * 设置toolBar背景图片
     */
    fun setTitleBarRes(res: Int) {
        setTitleBarVisibility(View.VISIBLE)
        rootViewBinding.titlebarLayout.setBackgroundResource(res)
        rootViewBinding.titlebarLayout.setPadding(0, 0, 0, 0)
    }

    /**
     * 设置是否显示返回
     */
    fun setBackBtnVisibility(visibility: Int) {
        rootViewBinding.ivBaseBack.visibility = visibility
    }

    /**
     * 设置返回按钮图标
     */
    fun setBackBtnRes(res: Int){
        setBackBtnVisibility(View.VISIBLE)
        rootViewBinding.ivBaseBack.setImageResource(res)
    }

    /**
     * 是否显示Title
     */
    fun setTextTitleVisibility(visibility: Int){
        rootViewBinding.tvBaseTitle.visibility = visibility
    }

    /**
     * 设置Title颜色
     */
    fun setTextTitleColor(color: Int) {
        setTextTitleVisibility(View.VISIBLE)
        rootViewBinding.tvBaseTitle.setTextColor(color)
    }

    /**
     * 设置Title
     */
    fun setTextTitle(text: String) {
        setTextTitleVisibility(View.VISIBLE)
        rootViewBinding.tvBaseTitle.text = text
    }

    /**
     * 设置Title对齐方式
     * 默认：Gravity.LEFT
     */
    fun setTextTitleGravity(gravity: Int){
        setTextTitleVisibility(View.VISIBLE)
        rootViewBinding.tvBaseTitle.gravity = gravity
    }

    /**
     * 设置是否显示右侧图标
     */
    fun setRightBtnVisibility(visibility: Int){
        rootViewBinding.ivFun.visibility = View.VISIBLE
    }

    /**
     * 设置右侧按钮图标
     */
    fun setRightBtnRes(res: Int){
        setRightBtnVisibility(View.VISIBLE)
        rootViewBinding.ivFun.setImageResource(res)
    }

    /**
     * 设置是否显示切换语言图标
     */
    fun setLangBtnVisibility(visibility: Int){
        rootViewBinding.imgLang.visibility = visibility
    }

    /**
     * 设置切换语言图标
     */
    fun setLangBtnRes(res: Int){
        setLangBtnVisibility(View.VISIBLE)
        rootViewBinding.imgLang.setImageResource(res)
    }

    override fun showLoading() {
        val act = context as BaseActivity
        act.showLoading()
    }

    fun showLoading(isTOuchOut: Boolean, isCancel: Boolean) {
        val act = context as BaseActivity
        act.showLoading(isTOuchOut,isCancel)
    }

    override fun hideLoading() {
        val act = context as BaseActivity
        act.hideLoading()
    }

    override fun showToast(s: CharSequence?) {
        showToast(s.toString())
    }

    /**
     * toast
     * @param msg
     */
    fun showToast(msg: String?) {
        val act = context as BaseActivity
        act.showToast(msg)
    }

    /**
     * toast
     * @param msg
     */
    fun showToastDuration(msg: String?, duration: Int) {
        val act = context as BaseActivity
        act.showToastDuration(msg, duration)
    }

    /**
     * 页面跳转（不带参数）
     *
     * @param T
     */
    open fun onIntent(T: Class<*>?) {
        val act = context as BaseActivity
        act.onIntent(T)
    }

    /**
     * 页面跳转（带参数）
     *
     * @param T
     * @param bundle
     */
    open fun onIntent(T: Class<*>?, bundle: Bundle?) {
        val act = context as BaseActivity
        act.onIntent(T,bundle)
    }

    /* 隐藏键盘 */
    fun onHideSoftInput(v: View) {
        val act = context as BaseActivity
        act.onHideSoftInput(v)
    }

    /* 显示键盘 */
    fun onShowSoftInput(v: View) {
        val act = context as BaseActivity
        act.onShowSoftInput(v)
    }

    /* 返回 */
    fun onBackAction() {
        if (!isInterceptBack) { //不拦截，直接返回
            mActivity?.finish()
        }
    }

    /* 是否拦截返回按钮 */
    fun isInterceptBack(isBackExit: Boolean) {
        isInterceptBack = isBackExit
    }

}
